package com.nand.blockdemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.nand.blockdemo.R;
import com.nand.blockdemo.bean.GridItem;
import com.nand.blockdemo.bean.ModuleBean;
import com.nand.blockdemo.ui.FunctionActivity;
import com.nand.blockdemo.ui.MainActivity;
import com.nand.blockdemo.ui.feedback.FeekActivity;
import com.nand.library.base.utils.MsgUtil;
import com.nand.module.function.bean.EqDatailBean;
import com.nand.module.function.ui.ClampingActivity;
import com.nand.module.function.utils.LogUtils;
import com.nand.module.user.ui.UserInfoActivity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
    private Context mContext;
    private List<ModuleBean.ChildBean> list = new ArrayList<>();
    private RequestOptions error;

    public GridAdapter(Context context) {
        this.mContext = context;
    }

    public void setList(List<ModuleBean.ChildBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public GridAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(@NonNull GridAdapter.ViewHolder holder, final int position) {
        //final int s = position;
        ViewHolder myViewHolder = (ViewHolder) holder;
        if (7 == position) {
            myViewHolder.tv.setText("更多");
            myViewHolder.iv.setImageResource(R.mipmap.home_equipment_more_icon);

        } else {
            ModuleBean.ChildBean childBean = list.get(position);
            myViewHolder.tv.setText(childBean.getModuleName());
            error = new RequestOptions().placeholder(R.mipmap.image_error).error(R.mipmap.image_error);
            Glide.with(mContext).asBitmap().load(childBean.getModuleUrl()).apply(error).into(myViewHolder.iv);
            LogUtils.e("ac================" + childBean.getModuleUrl());
        }

        myViewHolder.myItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position == 7) {
                    Intent in = new Intent(mContext, FunctionActivity.class);
                    mContext.startActivity(in);
                } else {
                    int code = list.get(position).getCode();
                    switch (code) {
                        case 4:  //设备保养
                            ARouter.getInstance().build("/function/device").withInt("type", 3).navigation();
                            break;
                        case 5:  //设备报修
                            ARouter.getInstance().build("/function/device").withInt("type", 2).navigation();
                            break;
                        case 6:  //设备台账
                            ARouter.getInstance().build("/function/device").withInt("type", 0).navigation();
                            break;
                        case 7:  //设备巡检
                            ARouter.getInstance().build("/function/device").withInt("type", 1).navigation();
                            break;
                        case 9:  //来料
                            ARouter.getInstance().build("/function/iqc").withInt("type", 0).navigation();
                            break;
                        case 10:  //PQC
                            ARouter.getInstance().build("/custom/PQCList").withInt("type", 1).navigation();
                            // MsgUtil.toast("这里是PQC");
                            break;
                        case 16:  //PQC复查
                            ARouter.getInstance().build("/custom/PQCList").withInt("type", 2).navigation();
                            //     MsgUtil.toast("这里是PQC");
                            break;
                        case 11:  //OQC
                            //     MsgUtil.toast("这里是OQC");
                            break;
                        case 14:  //IQC送检
                            ARouter.getInstance().build("/function/iqc").withInt("type", 1).navigation();
                            break;
                        case 15:  //IQC不合格
                            ARouter.getInstance().build("/function/iqc").withInt("type", 2).navigation();
                            break;
                        case 13:  //生产异常
                            ARouter.getInstance().build("/function/eqair/list").withInt("type", 1).navigation();
                            break;
                        case 20: //生产执行
                            ARouter.getInstance().build("/custom/PExecution").navigation();
                            break;
                        case 18: //物流看板
                            ARouter.getInstance().build("/function/mmm").navigation();
                            break;
                        case 19:  //工装管理
                            ARouter.getInstance().build("/function/clamping/list").navigation();
                            break;
                        case 2://意见反馈
                            Intent intent = new Intent(mContext, FeekActivity.class);
                            mContext.startActivity(intent);
                            break;
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            if (8 < list.size()) {
                return 8;
            } else {
                return list.size();
            }
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        ImageView iv;
        View myItemView;

        public ViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.home_item_text);
            iv = (ImageView) itemView.findViewById(R.id.home_item_icon);
            myItemView = itemView;
        }
    }
}
